package org.hxzon.cyclone.plinks;

import java.util.List;

import org.hxzon.cyclone.Vector3;
import org.hxzon.cyclone.pcontacts.ParticleContact;

//连杆约束，其中一点固定
public class ParticleRodConstraint extends ParticleConstraint {
    public double length;

    public int addContact(List<ParticleContact> contacts) {
        double currentLen = currentLength();

        if (currentLen == length) {
            return 0;
        }

        ParticleContact contact = new ParticleContact();
        contacts.add(contact);

        contact.particle[0] = particle;
        contact.particle[1] = null;

        Vector3 normal = anchor.minus(particle.getPosition());
        normal.normalise();

        if (currentLen > length) {
            contact.contactNormal = normal;
            contact.penetration = currentLen - length;
        } else {
            contact.contactNormal = normal.multi(-1);
            contact.penetration = length - currentLen;
        }

        contact.restitution = 0;

        return 1;
    }

}
